package com.one.shopping.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.one.shopping.AppContext;
import com.one.shopping.R;
import com.one.shopping.api.OneApi;
import com.one.shopping.entity.Result;
import com.one.shopping.entity.Users;
import com.one.shopping.ui.baseActivity.BaseActivity;
import com.one.shopping.util.CommonUtils;
import com.one.shopping.util.GsonUtils;

import org.apache.http.Header;

/**
 * @author 柯尊诚
 * @version 1.0
 * @date 创建时间：2016/1/8 16:41
 */
public class LoginActivity extends BaseActivity{

    private Button loginBtn;
    private EditText nameField;
    private EditText pwdField;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("登录");
        loginBtn = findView(R.id.login_btn);
        nameField = findView(R.id.login_name_et);
        pwdField = findView(R.id.login_passwd_et);
    }

    @Override
    protected void initListener() {
        super.initListener();
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
    }

    private void login() {
        String name = nameField.getText().toString().trim();
        String pwd = pwdField.getText().toString().trim();
        if (CommonUtils.isNullOrEmpty(name)) {
            nameField.setError("用户名不能为空");
            return;
        }
        if (CommonUtils.isNullOrEmpty(pwd)) {
            pwdField.setError("密码不能为空");
            return;
        }
        Users users = new Users();
        users.setName(name);
        users.setPasswd(pwd);

        OneApi.login(users, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Result result = GsonUtils.fromJson(new String(bytes), Result.class);
                if (result.getCode() == 200) {
                    Users resultUser = GsonUtils.fromJson(result.getData(), Users.class);
                    Log.d("xxx", resultUser + "");
                    AppContext.getInstance().saveLoginInfo(resultUser);
                    if (resultUser.getType() == 0) {
                        makeToast("欢迎管理员" + resultUser.getName() + "登录！");
                        Intent intent = new Intent(LoginActivity.this, AdminActivity.class);
                        startActivity(intent);
                    } else {
                        makeToast("欢迎收银员" + resultUser.getName() + "登录！");
                        Intent intent = new Intent(LoginActivity.this, TabHostActivity.class);
                        startActivity(intent);
                    }
                    finish();
                } else {
                    makeToast(result.getMessage());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                makeToast("网络错误！");
            }
        });
    }
}
